<template>
  <div id="fromBody">
    <div class="logbox">
      <div class="imgBox">
        <el-image
          class="img"
          src="https://destiny001.gitee.io/admin-dist/img/logo.b5a855ee.png"
        ></el-image>
      </div>
      <el-form ref="form" :model="form" :rules="rules" class="form">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user"

          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="form-item">
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button native-type="button" type="info" @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { onLogin } from '@/api/login'

export default {
  name: 'login',
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名称',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 6,
            message: '长度在 1 到 6 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 12,
            message: '长度在 1 到 12 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.form.username = ''
      this.form.password = ''
    },
    async submitForm () {
      const res = await onLogin(this.form)
      const token = res.data.token
      this.$store.commit('setUser', token)
      if (res.meta.status === 200) {
        this.$message({
          message: res.meta.msg,
          type: 'success'
        })
        await this.$router.push('/')
      } else {
        this.$message.error(res.meta.msg)
      }
    },
    open4 () {

    }
  }
}
</script>

<style lang="less" scoped>
#fromBody {
  position: relative;
  background-color: #2b4b6b;
  width: 100%;
  height: 100%;

  .logbox {
    position: absolute;
    width: 450px;
    height: 300px;
    background-color: #fff;
    top: 50%;
    left: 50%;
    margin-top: -150px;
    margin-left: -225px;
    border-radius: 5px;
    padding: 0 20px;

    .imgBox {
      background-color: #fff;
      width: 152px;
      height: 152px;
      position: absolute;
      left: 50%;
      top: -76px;
      margin-left: -76px;
      text-align: center;
      line-height: 152px;
      border-radius: 50%;
      padding-top: 11px;
      box-shadow: 0 0 10px #ddd;

      .img {
        width: 130px;
        height: 130px;
      }
    }

    .form {

      position: absolute;
      bottom: 0;
      height: 189px;
      width: 410px;

      .form-item {
        text-align: right;
      }
    }
  }
}
</style>
